package com.tanguy.user.ui.window

import android.content.Context
import android.view.View
import com.tanguy.user.R
import com.tanguy.user.base.BasePopupWindow
import com.tanguy.user.utlis.TimeUtils
import com.tanguy.user.utlis.extend.click
import com.tanguy.user.utlis.extend.toTimeString
import com.weigan.loopview.LoopView
import org.jetbrains.anko.find

/**
 * Created by dab on 2017/10/30 0030 17:38
 * 选择时间的窗口
 */
class SelectDateWindow(context: Context, private val sureClick: (timestamp: Long, timeString: String, errMsg: String?) -> Unit) : BasePopupWindow(context) {
    override fun setContentRes(): Int = R.layout.window_select_data
    private val minuteList by lazy {
        (0 until 60).mapTo(ArrayList(60)) { String.format("%02d分", it) }
    }
    private val hourList by lazy {
        (0 until 24).mapTo(ArrayList(24)) { String.format("%02d时", it) }
    }
    val dataTime = arrayListOf("",TimeUtils.getDays(0), TimeUtils.getDays(1), TimeUtils.getDays(2))
    val dataTimeShow = arrayListOf("现在","今天", "明天", "后天")
    private lateinit var mLoopOne: LoopView
    private lateinit var mLoopTwo: LoopView
    private lateinit var mLoopThree: LoopView
    override fun initView(view: View) {
        super.initView(view)
//        val s = dataTime[mLoopOne.selectedItem] + hourList[mLoopTwo.selectedItem]
//        val time: String = dataTime[mLoopOne.selectedItem] + hourList[mLoopTwo.selectedItem] + minuteList[mLoopThree.selectedItem]

        view.apply {
            mLoopOne = find(R.id.lv_pop_one)
            mLoopTwo = find(R.id.lv_pop_two)
            mLoopThree = find(R.id.lv_pop_three)
            click(R.id.tv_select_cancel) { this@SelectDateWindow.dismiss() }
            click(R.id.tv_select_confirm) {
                if (mLoopOne.selectedItem == 0) {
                    sureClick.invoke(System.currentTimeMillis(), "现在", null)
                    this@SelectDateWindow.dismiss()
                    return@click
                }
                val time: String = dataTime[mLoopOne.selectedItem] + hourList[mLoopTwo.selectedItem] + minuteList[mLoopThree.selectedItem]
                val timestamp = TimeUtils.dateToStamp(time)

                when {
                    timestamp == 0L -> {
                        //获取失败
                        sureClick.invoke(timestamp, "", "获取时间失败")
                    }
                    timestamp < System.currentTimeMillis() -> {
                        //选择之前的时间了
                        sureClick.invoke(timestamp, "", "不能选择之前的时间")
                    }
                    else -> {
                        val timeString = dataTimeShow[mLoopOne.selectedItem] + timestamp.toTimeString(TimeUtils.TIME_MM_DD_HH_MM)
                        //正确时间
                        sureClick.invoke(timestamp, timeString, null)
                        this@SelectDateWindow.dismiss()
                    }
                }
            }

        }

    }

    override fun initEvent() {
        super.initEvent()
        initLoop(mLoopOne, dataTimeShow)
        initLoop(mLoopTwo, hourList)
        initLoop(mLoopThree, minuteList)
        mLoopOne.setNotLoop()
        mLoopOne.setCurrentPosition(1)
        mLoopTwo.setCurrentPosition(hourList.size/2)
        mLoopThree.setCurrentPosition(minuteList.size/2)
        mLoopOne.setListener {
            mLoopTwo.visibility=if (it==0)View.INVISIBLE else View.VISIBLE
            mLoopThree.visibility=if (it==0)View.INVISIBLE else View.VISIBLE
        }
    }

    private fun initLoop(loopView: LoopView, arrayList: ArrayList<String>) {
        loopView.setItems(arrayList)
//        loopView.setNotLoop()
        loopView.setInitPosition(0)
        loopView.setTextSize(20f)

    }


}